#!/bin/bash
set -euo pipefail
BaseDir=$(cd "$(dirname "$0")"; pwd)
cd ${BaseDir}

echo "pyenv安装"
export PYTHON_BUILD_MIRROR_URL=https://jedore.netlify.app/tools/python-mirrors/
bash ./pyenv-installer.sh

cat >> ~/.bash_profile <<EOF
export PYENV_ROOT="\$HOME/.pyenv"
[[ -d \$PYENV_ROOT/bin ]] && export PATH="\$PYENV_ROOT/bin:\$PATH"
eval "\$(pyenv init -)"
EOF

source  ~/.bash_profile

echo "安装python的版本"
gcc ||{
    echo "gcc make工具要安装好 gcc make 或者 dnf group list installed | grep Development"
    dnf install -y zlib-devel  bzip2-devel ncurses-devel libffi-devel readline-devel openssl-devel
    python -c "import bz2; import curses; import ctypes; import ssl; import readline"

    dnf install -y sqlite-devel xz-devel
    python -c "import sqlite3; import lzma" 
}

# export PYTHON_BUILD_MIRROR_URL=https://jedore.netlify.app/tools/python-mirrors/; pyenv install -v 3.12.0

export PYTHON_BUILD_MIRROR_URL=https://jedore.netlify.app/tools/python-mirrors/
# curl -k -fL -o /tmp/Python-3.12.0.tar.xz "https://registry.npmmirror.com/-/binary/python/3.12.0/Python-3.12.0.tar.xz"
curl -k -fL -o /tmp/Python-3.12.0.tar.xz "https://mirrors.aliyun.com/python/Python-3.12.0.tar.xz"
mkdir -p ~/.pyenv/cache
mv /tmp/Python-3.12.0.tar.xz  ~/.pyenv/cache/
pyenv install -v 3.12.0
pyenv versions
:<<EOF
# pyenv versions 显示如下
* system (set by /root/.pyenv/version)
  3.12.0
EOF

cat <<EOF
切换python的不同版本安装例子
pyenv global 3.8.0

cd /path/to/your/project  
pyenv local 3.12.0
EOF

echo "done"